/*
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: FinanceListFragment.java
 *   @Date: 19-4-24 下午3:33
 *   @Author: ppzhao
 *   @Description:
 *   @Copyright（C）: 2019 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 */

package io.bhex.app.finance.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import io.bhex.app.R;
import io.bhex.app.base.BaseFragment;
import io.bhex.app.finance.adapter.FinanceListAdapter;
import io.bhex.app.finance.presenter.FinanceListFragmentPresenter;
import io.bhex.app.utils.IntentUtils;
import io.bhex.app.view.TopBar;
import io.bhex.baselib.constant.AppData;
import io.bhex.baselib.utils.PixelUtils;
import io.bhex.sdk.account.UserInfo;
import io.bhex.sdk.finance.bean.FinanceBean;

public class FinanceListFragment extends BaseFragment<FinanceListFragmentPresenter,FinanceListFragmentPresenter.FinanceListUI> implements FinanceListFragmentPresenter.FinanceListUI, OnRefreshListener {
    private SmartRefreshLayout swipeRefresh;
    private RecyclerView recyclerView;
    private FinanceListAdapter adapter;
    private View emptyView;
    private TopBar topBar;

    @Override
    protected FinanceListFragmentPresenter.FinanceListUI getUI() {
        return this;
    }

    @Override
    protected FinanceListFragmentPresenter createPresenter() {
        return new FinanceListFragmentPresenter();
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_finance_list_layout,null,false);
    }

    @Override
    protected void initViews() {
        super.initViews();
        topBar = viewFinder.find(R.id.topBar);
        this.setTopbarNight(topBar);
        topBar.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserInfo.isLogin(getActivity(),AppData.INTENT.LOGIN_CALLER_TRADE)) {
                    IntentUtils.goCoinPlusOrders(getActivity());
                }
            }
        });
        swipeRefresh = viewFinder.find(R.id.swipeRefresh);
        swipeRefresh.setOnRefreshListener(this);
        recyclerView = viewFinder.find(R.id.recyclerView);

        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        emptyView = layoutInflater.inflate(R.layout.empty_layout, swipeRefresh, false);
        ViewGroup.LayoutParams layoutParams = emptyView.getLayoutParams();
        layoutParams.height = PixelUtils.dp2px(200);
        emptyView.setLayoutParams(layoutParams);
    }

    @Override
    public void showFinanceList(List<FinanceBean> data) {
        if (adapter == null) {
            adapter = new FinanceListAdapter(getActivity(),data);
            adapter.isFirstOnly(false);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setItemAnimator(new DefaultItemAnimator());

            adapter.setEmptyView(emptyView);
            recyclerView.setAdapter(adapter);
            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    FinanceBean financeBean = (FinanceBean) adapter.getData().get(position);
                    IntentUtils.goFinanceDetail(getActivity(),financeBean);

                }
            });
        } else {
            adapter.setNewData(data);
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        getPresenter().getFinanceList();
        refreshLayout.finishRefresh(1000);
    }
}
